﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;

namespace 基于WPF的电缆采样数据分析系统.Models.Dbs.DbService
{
    /// <summary>
    /// 用户使用记录
    /// </summary>
    public class UserUsedRecordService
    {
        /// <summary>
        /// 单例模型
        /// </summary>
        public static UserUsedRecordService Instance = new UserUsedRecordService(App.GetService<TDRDbContext>());

        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly TDRDbContext db;

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="_db"></param>
        public UserUsedRecordService(TDRDbContext _db)
        {
            db = _db;
        }


        /// <summary>
        /// 根据用户id增加用户使用记录
        /// </summary>
        /// <param name="Uid"></param>
        /// <returns></returns>
        public RetResult AddUserUsedRecord(int Uid)
        {
            try
            {
                UserUsedRecord userUsedRecord = new UserUsedRecord();
                userUsedRecord.Uid = Uid;
                userUsedRecord.LoginDateTime = DateTime.Now;

                db.UserUsedRecords.Add(userUsedRecord);

                int rows = db.SaveChanges();

                if (rows > 0)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {
                return RetResult.Fail;
            }
        }

        /// <summary>
        /// 获取所有的用户登录记录
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<UserUsedRecord> GetUserUsedRecords()
        {

            try
            {

                ObservableCollection<UserUsedRecord> userUsedRecords = new ObservableCollection<UserUsedRecord>(db.UserUsedRecords);

                if (userUsedRecords.Count > 0)
                {
                    return userUsedRecords;
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 根据起始时间和截止时间获取用户登录记录
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="deadline"></param>
        /// <returns></returns>
        public ObservableCollection<UserUsedRecord> GetUserUsedRecordsByTime(DateTime startTime, DateTime deadline)
        {

            try
            {

                ObservableCollection<UserUsedRecord> userUsedRecords = new ObservableCollection<UserUsedRecord>(db.UserUsedRecords.Where(r => r.LoginDateTime >= startTime 
                && r.LoginDateTime <= deadline));

                if (userUsedRecords.Count > 0)
                {
                    return userUsedRecords;
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
